#!/usr/bin/env  python
#coding=utf-8

import os
import sys
import urllib2
import time
import multiprocessing
from  pyinotify import  WatchManager, Notifier, ProcessEvent, IN_DELETE, IN_CREATE,IN_MOVED_TO,IN_MOVED_FROM

class MonitorFile(ProcessEvent):
    def process_IN_CREATE(self, event):  #文件被创建后要执行的动作
        url = "http://upgrade.box.tv.sohu.com/update/p/"  + os.path.join(event.name)
        file = os.path.join(event.path, event.name)
        time.sleep(10)
        main(url,file)

    def process_IN_DELETE(self, event):  #文件被删除后要执行的动作
        url1 = "http://upgrade.box.tv.sohu.com/purge/update/p/" + os.path.join(event.name)
        time.sleep(5)
        purge(url1)

def main(url,file):
        cmd1 = "chmod 644 %s" %file
        cmd = "curl -o /dev/null %s" %url
        os.system(cmd1)
        os.system(cmd)

def purge(url):
        cmd = "curl -o /dev/null %s" %url
        os.system(cmd)

path = "/data/update/p/"  #要监控的文件夹
wm = WatchManager()
mask = IN_DELETE|IN_CREATE|IN_MOVED_TO|IN_MOVED_FROM
notifier = Notifier(wm, MonitorFile ())
wm.add_watch(path, mask,rec=True)
  

if __name__ == "__main__":

    while True:
        try :
            notifier.process_events()
            if notifier.check_events():
                notifier.read_events()
        except KeyboardInterrupt:
            notifier.stop()
            break